package com.kangnam.mongo;

import com.kangnam.mongo.MongoManager;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.Mongo;

public class AddBuyList {
	private static MongoManager mongoManager = null;
	private static String DB_NAME;
	private static String C_INFO = "customer";
//	private String test1 = "12345",
//			 	   test2 = "45678",
//			       test3 = "00000";
//			
//	private	String testLoginId = "200601281";
	
	public AddBuyList(String dbName, String host)
	{
		mongoManager = MongoManager.getInstance();
		mongoManager.createPool(host);
		DB_NAME = dbName;	
	}

	public static String AddProduct(String collection, String field, String value, String add_filed, String add_value){
		
		Mongo mongo = null;
		DBObject obj = null;
		
		try{
			mongo = mongoManager.getConnection();
			DB db = mongo.getDB(DB_NAME);
			
			BasicDBObject query = new BasicDBObject();
			query.append(field, value);
			
			DBCollection coll = db.getCollection(collection);		
			obj = coll.findOne(query);
			
			obj.put(add_filed, add_value);
			coll.insert(obj);
			coll.save(obj);
//			DBCursor cur = coll.find(query);
			
			
		} finally {
			mongoManager.close(mongo);
		}
		return obj.toString();
		//		return obj.get("_id").toString();
		
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		AddBuyList InputFT = new AddBuyList("db", "127.0.0.1");
		String list; 
		list=AddProduct(C_INFO,"c_LoginID", "200601281","c_Buy_List[0]", "123456");
		list=AddProduct(C_INFO,"c_LoginID", "200601281","c_Buy_List[1]", "234567");
		System.out.println(list);

	}

}
